# -*- coding: utf-8 -*- 
# @Time : 2021/4/4 23:31 
# @Author : Dong 
# @File : ip_spider_weixin_sougou.py

import requests
from requests.exceptions import ConnectionError
from urllib.parse import urlencode
from pyquery import PyQuery as pq

base_url = 'https://weixin.sogou.com/weixin?'
headers = {
    'cookie': 'ABTEST=0|1617542227|v1; IPLOC=CN3301; SUID=9E6BBA3C3E18960A000000006069BC53; SUID=9E6BBA3C1639960A000000006069BC53; weixinIndexVisited=1; SUV=00F4E6653CBA6B9E6069BC54A5C33166; SNUID=27D30384B9BC069CDE86F03EB960AFF8; ppinf=5|1617550616|1618760216|dHJ1c3Q6MToxfGNsaWVudGlkOjQ6MjAxN3x1bmlxbmFtZTo3OkNoaWxsZWR8Y3J0OjEwOjE2MTc1NTA2MTZ8cmVmbmljazo3OkNoaWxsZWR8dXNlcmlkOjQ0Om85dDJsdUZkMDRidzBBb1VORTRmejJCN3lxRTRAd2VpeGluLnNvaHUuY29tfA; pprdig=D71PAbkLCDe5UPNdAG8TIjhAh7idFbtFCDgoLjlcnu3VTwL9Y18PIi5ipaeAeL8q96w95S_liX1x-dOlJFOXJLCySHgo00M5eKDnMHf2trrre7nggK_05NFWoxWbnBoqplfXKiu44MZWsibDBiAh1zDE1hBaUgBsN9f-pvM9NWk; ppinfo=51422cb52a; passport=5|1617550616|1618760216|dHJ1c3Q6MToxfGNsaWVudGlkOjQ6MjAxN3x1bmlxbmFtZTo3OkNoaWxsZWR8Y3J0OjEwOjE2MTc1NTA2MTZ8cmVmbmljazo3OkNoaWxsZWR8dXNlcmlkOjQ0Om85dDJsdUZkMDRidzBBb1VORTRmejJCN3lxRTRAd2VpeGluLnNvaHUuY29tfA|3ed7c6cf02|D71PAbkLCDe5UPNdAG8TIjhAh7idFbtFCDgoLjlcnu3VTwL9Y18PIi5ipaeAeL8q96w95S_liX1x-dOlJFOXJLCySHgo00M5eKDnMHf2trrre7nggK_05NFWoxWbnBoqplfXKiu44MZWsibDBiAh1zDE1hBaUgBsN9f-pvM9NWk; sgid=04-50026127-AWBp3RiaNXdNNjEPNyspic4AI; ppmdig=1617597314000000b7ec661bd1b27f283f94ff2f89fbb0aa',
    'referer': 'https://open.weixin.qq.com/',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36'

}

def get_url(url):
    try:
        response = requests.get(url,headers=headers,allow_redirects=False)   # allow_redirects=False不让他自己处理302
        if response.status_code == 200:
            return response.text
        if response.status_code == 302:
            # need proxy
            print('302')
    except ConnectionError:
        get_url(url)

def get_index(keyword,page):
    data = {
        'query': keyword,
        'type': 2,
        'page': page,
}
    queries = urlencode(data)
    url = base_url + queries
    html = get_url(url)
    return html

def parse_one_index(html):
    doc = pq(html)
    items = doc('.news-box .news-list li .txt-box h3 a').items()
    for i in items:
        yield i.attr('href')

def main():
    for i in range(2):
        html = get_index('风景', i)
        if html:
            article_urls = parse_one_index(html)
            # print(article_urls)
            for article_url in article_urls:
                print(article_url)


if __name__ == '__main__':
    main()









